import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * dataSourceDriver-数据源驱动
 * @date: 2019-08-22
 */
import { queryMapIdpValue } from 'services/api';
import { getResponse, createPagination } from 'utils/utils';
import { fetchDriversList as _fetchDriversList, fetchDriverById as _fetchDriverById, createDriver as _createDriver, updateDriver as _updateDriver, removeDriver as _removeDriver } from "../services/datasourceDriverService";
export default {
  namespace: 'dataSourceDriver',
  // dataSourceDriver 数据源驱动

  /**
   * state
   */
  state: {
    driverList: [],
    // 数据源驱动列表
    pagination: {},
    // 分页
    dataSourceTypeList: [] // 数据源列表

  },
  effects: {
    // 获取初始化数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, lovCodes, res, typeList;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              lovCodes = payload.lovCodes;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(queryMapIdpValue, lovCodes);

            case 6:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              typeList = res.typeList;
              _context.next = 11;
              return put({
                type: 'updateState',
                payload: {
                  dataSourceTypeList: typeList
                }
              });

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 查询数据驱动列表
    fetchDriversList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDriversList(_ref3, _ref4) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchDriversList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(_fetchDriversList, payload);

            case 5:
              _context2.t1 = _context2.sent;
              result = (0, _context2.t0)(_context2.t1);

              if (!result) {
                _context2.next = 10;
                break;
              }

              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  pagination: createPagination(result),
                  driverList: result.content
                }
              });

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchDriversList);
    }),
    // 通过DriverId查询数据驱动
    fetchDriverById: /*#__PURE__*/_regeneratorRuntime.mark(function fetchDriverById(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function fetchDriverById$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_fetchDriverById, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, fetchDriverById);
    }),
    // 创建数据驱动
    createDriver: /*#__PURE__*/_regeneratorRuntime.mark(function createDriver(_ref7, _ref8) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createDriver$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_createDriver, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, createDriver);
    }),
    // 更新数据驱动
    updateDriver: /*#__PURE__*/_regeneratorRuntime.mark(function updateDriver(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateDriver$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_updateDriver, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, updateDriver);
    }),
    // 删除数据驱动
    removeDriver: /*#__PURE__*/_regeneratorRuntime.mark(function removeDriver(_ref11, _ref12) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function removeDriver$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call;
              _context6.next = 4;
              return call(_removeDriver, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, removeDriver);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref13) {
      var payload = _ref13.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};